package com.toogu.mazhangmen.util;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.toogu.mazhangmen.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ToastUtil {
    private static ToastUtil toastUtil;
    private int time;
    private Timer timer;
    private List<Data> dataList;
    private Activity activity;
    private LinearLayout root;

    class Data{
        String content;
        long timeSeconds;
        boolean isShow;
        public Data(String content){
            timeSeconds = System.currentTimeMillis();
            this.content=content;
        }
    }
    private ToastUtil(){
        time=4000;
        dataList=new ArrayList<>();
    }
    //DCL双检查锁机制（DCL：double checked locking）
    public static ToastUtil getToastUtil() {
        // 第一次检查OkHttpClient是否被实例化出来，如果没有进入if块
        if (toastUtil == null) {
            //加synchronized关键字实现同步
            synchronized (OkhttpUtil.class) {
                // 某个线程取得了类锁，实例化对象前第二次检查OkHttpClient是否已经被实例化出来，如果没有，才最终实例出对象
                if (toastUtil == null) {
                    toastUtil = new ToastUtil();
                }
            }
        }
        return toastUtil;
    }


    public void init(Activity activity){
        if (!activity.equals(this.activity)) {
            this.activity = activity;
            dataList.clear();
        }
        View decorView= activity.getWindow().getDecorView();
        FrameLayout contentParent = (FrameLayout) decorView.findViewById(android.R.id.content);
        FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(0,UiUtil.dip2px(20),0, 0);
        root = new LinearLayout(activity);
        root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        root.setGravity(Gravity.TOP|Gravity.RIGHT);
        root.setOrientation(LinearLayout.VERTICAL);
        contentParent.addView(root,layoutParams);
    }

    public void show(Activity activity) {
        init(activity);
        LinearLayout.LayoutParams  layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, UiUtil.dip2px(5),0, UiUtil.dip2px(5));
        layoutParams.gravity=Gravity.RIGHT|Gravity.BOTTOM;
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                while (dataList.size() > 3) dataList.remove(0);
                while (dataList.size() != 0 && (System.currentTimeMillis() - dataList.get(0).timeSeconds) > time) dataList.remove(0);
                activity.runOnUiThread(()->{
                    root.removeAllViews();
                    for(Data data:dataList){
                        root.addView(createText(data.content),layoutParams);
                    }
                });
            }
        }, 0, 1000);
    }
    public void close(){
        timer.cancel();
        root.removeAllViews();
        dataList.clear();

    }

    private View createText(String name) {
        TextView textView = new TextView(activity);
        textView.setBackgroundResource(R.drawable.bg_frame);
        textView.setText(name);
        textView.setTextColor(Color.WHITE);
//        textView.setTextSize(16);
        textView.setPadding(UiUtil.dip2px(10), UiUtil.dip2px(10), UiUtil.dip2px(10), UiUtil.dip2px(10));
        return textView;
    }
    public void add(String content){
        dataList.add(new Data(content));
    }


}
